package com.ming.springcloud.consumer.controller;

import com.alibaba.fastjson.JSONObject;

import com.ming.springcloud.consumer.service.DeviceService;
import com.ming.springcloud.consumer.util.ResponseEntity;

import com.ming.springcloud.consumer.vo.Devices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/device_hystric")
public class DeviceHystricController {
    @Autowired
    private DeviceService deviceService;


    @RequestMapping(value = "findAllDevices", method = RequestMethod.GET)
    public ResponseEntity findAllStudent1(){
        String userJSON= deviceService.findAllDevices("ming");
        return  ResponseEntity.success().add("pageInfo",userJSON);
    }

    @RequestMapping(value = "findDevicesById/{id}", method = RequestMethod.GET)
    public ResponseEntity findStudentsById(@PathVariable("id")Integer id){
        String userJSON= deviceService.findDevicesById("ming",id);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);

        //获取回传的josn数据
        String deviceId=object.get("deviceId").toString();
        String deviceName=object.get("deviceName").toString();
        String deviceAddress=object.get("deviceAddress").toString();
        String deviceType=object.get("deviceType").toString();
        String remarks=object.get("remarks").toString();
        String serviceName=object.get("serviceName").toString();
        String ids=object.get("id").toString();

        Devices devices=new Devices();
        devices.setId(Integer.parseInt(ids));
        devices.setServiceName(serviceName);
        devices.setDeviceId(deviceId);
        devices.setDeviceName(deviceName);
        devices.setDeviceType(deviceType);
        devices.setDeviceAddress(deviceAddress);
        devices.setRemarks(remarks);


        return  ResponseEntity.success().add("user",devices);
    }

    @RequestMapping(value = "delDevice/{id}", method = RequestMethod.GET)
    public ResponseEntity delDevice(@PathVariable("id")Integer id){
        String userJSON= deviceService.delDevice("ming",id);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);
        Integer result =Integer.parseInt(object.get("code").toString());
        if(result>0){
            return ResponseEntity.success();
        }else{
            return ResponseEntity.error("删除失败");
        }
    }

    @PostMapping("/editDevice")
    public ResponseEntity addOrEditUser(Devices devices){
        if(devices==null){
            return ResponseEntity.error("参数错误");
        }
        if(devices.getId()==null){//新增
            String userJSON=deviceService.addDevice("ming",devices);

            JSONObject object = (JSONObject) JSONObject.parse(userJSON);
            Integer result =Integer.parseInt(object.get("code").toString());

            if(result>0){
                return ResponseEntity.success();
            }else{
                return ResponseEntity.error("新增失败");
            }
        }else{
            String userJSON=deviceService.editDevice("ming",devices);

            JSONObject object = (JSONObject) JSONObject.parse(userJSON);
            Integer result =Integer.parseInt(object.get("code").toString());

            if(result>0){
                return ResponseEntity.success();
            }else{
                return ResponseEntity.error("修改失败");
            }
        }
    }
}
